A web application for creating and sharing data visualizations.
data(mtcars)
plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg, type = "scatter")
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: package 'bindrcpp' was built under R version 3.5.1
#Scatterplot with color on discrete var
plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg, type = "scatter", color=as.factor(mtcars$cyl))
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
#Scatterplot with color on cont' var
plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg, type = "scatter", color=mtcars$disp)
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
#Scatter plot sizing
plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg, type = "scatter", color=as.factor(mtcars$cyl), size=mtcars$hp)
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
# 3D plotting
set.seed(2016-07-21)
temp <- rnorm(100, mean=30, sd=5)
pressure <- rnorm(100)
dtime <- 1:100
plot_ly(x=temp, y=pressure, z=dtime, type="scatter3d", mode="markers", color=temp)
data("airmiles")
plot_ly(x=time(airmiles), y=airmiles, mode="lines")
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
#Multi line graph
data("EuStockMarkets")
stocks <- as.data.frame(EuStockMarkets) %>%
gather(index, price) %>%
mutate(time=rep(time(EuStockMarkets),4))
plot_ly(stocks, x=stocks$time, y=stocks$price, color=stocks$index, mode="lines")
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
#Histogram
plot_ly(x=precip, type="histogram")
#Boxplots
plot_ly(iris, y=iris$Petal.Length, color=iris$Species, type="box")
#Heatmap
terrain1 <- matrix(rnorm(100*100), nrow=100, ncol=100)
plot_ly(z=terrain1, type="heatmap")
#3D surface
terrain2 <- matrix(sort(rnorm(100*100)), nrow=100, ncol=100)
plot_ly(z=terrain2, type="surface")
state_pop <- data.frame(State=state.abb, Pop=as.vector(state.x77[,1]))
state_pop$hover <- with(state_pop, paste(state_pop$State, "<br>", "Population:", state_pop$Pop))
borders <- list(color=toRGB("red"))
map_options <- list(
scope = "usa",
projection = list(type="albers usa"),
showlakes =TRUE,
lakecolor = toRGB('white')
)
plot_ly(state_pop, z=state_pop$Pop, text=state_pop$hover, locations=state_pop$State, type='choropleth', locationmode='USA-states', color=state_pop$Pop, colors='Blues', marker=list(line=borders)) %>%
layout(title='US Population in 1975', geo=map_options)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000),]
p <- ggplot(data=d, aes(x=carat, y=price))+
geom_point(aes(text=paste("Clarity:", clarity)), size=4)+
geom_smooth(aes(colour=cut, fill=cut)) +facet_wrap(~cut)
## Warning: Ignoring unknown aesthetics: text
(gg<-ggplotly(p))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'